package com.sophialex.health.record.views;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.httpclient.util.DateUtil;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.person.vo.HrIssue;
import com.sophialex.health.person.vo.HrPerson;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.action.HrIssueAddAction;
import com.sophialex.health.record.action.HrIssueEditAction;
import com.sophialex.health.record.common.util.DataSource;
import com.sophialex.health.record.common.util.EventType;
import com.sophialex.health.record.common.util.HealthRecordListenerRegister;
import com.sophialex.health.system.lktable.LKCellStyleProvider;
import com.sophialex.health.system.lktable.LKIGetJoinOutValue;
import com.sophialex.health.system.lktable.LKTable;
import com.sophialex.health.system.lktable.SWTX;
import com.sophialex.health.system.lktable.LKTableUtil.ECellEditType;

public class PersonBottomLeftView extends ViewPart implements IPropertyChangeListener{

	public static final String ID = "com.sophialex.health.record.views.PersonBottomLeftView"; 
	public LKTable lktable;
	private Action addHrIssueAction;
	private Action editHrIssueAction;
	
	private List<HrIssue> hrIssueList;
	public PersonBottomLeftView() {
	}

	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		lktable = new LKTable(parent, SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY
		      				| SWTX.WRAP | SWT.VERTICAL | SWT.HORIZONTAL);

		fillTable()	;
		createActions();
		initializeToolBar();
		initializeMenu();
		contributeToActionBars();
		HealthRecordListenerRegister.getInstance().addPropertyChangeListener(this);
	}
	
	public void fillTable() {
		lktable.doAddColumn(0, Messages.getString("Health.Question.0"), "issueName", 100, 
		    				SWTX.ALIGN_HORIZONTAL_CENTER, ECellEditType.textBox, false,
		    				false);
		lktable.doAddColumn(1, Messages.getString("Health.Question.1"), "happenDate", 80,
		    				SWTX.ALIGN_HORIZONTAL_CENTER, ECellEditType.textBox, false,
		    				false);
		lktable.setHeaderHeight(30);
		lktable.exeLKTable(HrIssue.class);
		lktable.setJoinOut(new LKIGetJoinOutValue() {
			public String getFieldValue(Object obj, String fieldName, Object byValue) {
				if ("happenDate".equals(fieldName)) { 
					 if(byValue!=null){
						 try {
							 Date date = new Date(Date.parse(byValue.toString()));
							 return DateUtil.formatDate(date,"yyyy-MM-dd");
						}
						catch(Exception e) {
							e.printStackTrace();
						}
					 }
				}
				return byValue == null ? "" : byValue.toString(); 
			}
		});
		lktable.setCellStyleProvider(new LKCellStyleProvider());
		lktable.setCanTraverse(false);
		setData();
	}
	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalToolBar(bars.getToolBarManager());
	}
	private void fillLocalToolBar(IToolBarManager manager) {
		manager.removeAll();
		manager.add(addHrIssueAction);
		manager.add(editHrIssueAction);
	}
	/**
	 * Create the actions.
	 */
	private void createActions() {
		addHrIssueAction =  new HrIssueAddAction(getSite().getWorkbenchWindow(),this);
		editHrIssueAction =  new HrIssueEditAction(getSite().getWorkbenchWindow(),lktable,this);
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
	
	public void setData() {
		try {
			HrIssue q = new HrIssue();
			HrPerson currentPerson = SuperContext.currentPerson;
			if(currentPerson != null){
				q.setPersonId(currentPerson.getId());
				hrIssueList = DataSource.hrIssueService.findPageInfoHrIssue(q,null);
				lktable.setInput(hrIssueList);
			}else{
				lktable.setInput(null);
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void propertyChange(PropertyChangeEvent event) {
		if(event.getProperty().equals(EventType.REFRESH)){
			setData();
		}
		
	}
	
	public void refreshAdd(HrIssue hrIssue){
		if(hrIssueList == null){
			hrIssueList = new ArrayList<HrIssue>();
		}
		hrIssueList.add(hrIssue);
		lktable.setInput(hrIssueList);
		lktable.setSelection(hrIssue);
		lktable.redraw();
	}
	public void refreshUpdate(){
		lktable.setInput(hrIssueList);
		lktable.redraw();
	}
}

